=begin
导入抓取的点评至正式库
add by kuang 2010.09.05
 
=end
require 'rubygems'
require '../db_help/db_help'

class ImportCrawlComments
  
  def initialize
    @db_help_staging = DBHelp::DBHelp.create_staging()
    @db_help_tao     = DBHelp::DBHelp.create_tao_hidoudou()
  end
  
  def import
    0.upto((item_total / 1000 + 1)) do |i|
      max_item_id = 0
      
      alliances_num_iid(max_item_id).each do |iid|
        comments = @db_help_tao.query("select * from spider_item_comments where num_iid = #{iid['num_iid']} order by id")
        
        item_id = iid['item_id']
        puts "#{item_id} - #{iid['num_iid']}"
        comments.each_hash do |comment|
          
          sql_hash = {}
          sql_hash['table']        = 'item_crawl_comments'
          sql_hash['item_id']      = item_id
          sql_hash['user_name']     = comment['user_name']
          sql_hash['content']      = comment['content']
          sql_hash['comment_time'] = comment['created_at']
          sql_hash['source_site']  = 'taobao'
          sql = @db_help_staging.build_sql(sql_hash)
          @db_help_staging.query(sql)
        end #comments
        
        max_id(item_id)
      end #alliances
    end#items
  end

  def alliances_num_iid(max_item_id)
    ret = []
    results = @db_help_staging.query("select item_id, relation_id as num_iid from item_alliances where item_id > #{max_item_id} group by relation_id order by item_id asc limit 1000 ")
    results.each_hash do |item|
      ret << {'num_iid' => item["num_iid"], 'item_id' => item['item_id']}
    end
    ret
  end

  def item_total
    return @db_help_staging.query("select count(id) from items where id > #{max_id}").fetch_row()[0].to_i rescue 0
  end

  def max_id(max_id)
    if max_id.nil?
      @db_help_staging.query("select max_id from counter where counter_id = 1 ").fetch_row()[0].to_i rescue 0
    else
      @db_help_staging.query("update counter set max_id = #{max_id} where counter_id = 1")      
    end
  end
  
end
icc = ImportCrawlComments.new
icc.import

